//index.js
//获取应用实例
var app = getApp();

var scrollTimer
Page({
  data: {
    classifySeleted:'1',//默认为第一个
    cakeTypesTitle: [{ typeId: 1, typeName: '第一' }, { typeId: 2, typeName: '第二' }, { typeId: 3, typeName: '第三' }, { typeId: 4, typeName: '第四' }, { typeId: 5, typeName: '第5' }, { typeId: 6, typeName: '第6' }, { typeId: 7, typeName: '第7' }, { typeId: 8, typeName: '第8' }, { typeId: 9, typeName: '第9' }, { typeId: 10, typeName: '第10' }],
    cakeTypes:[
      { typeName: '第一', typeId: 1, productIds: [ { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }]},
      { typeName: '第二', typeId: 2, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第三', typeId: 3, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第四', typeId: 4, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第5', typeId: 5, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第6', typeId: 6, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第7', typeId: 7, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第8', typeId: 8, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第9', typeId: 9, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] },
      { typeName: '第10', typeId: 10, productIds: [{ imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }, { imgsrc: 'https://minstech-mp.oss-cn-hangzhou.aliyuncs.com/addons/yuniudao/timg.jpg' }] }

    ]

  },
 onShow:function(){
   //这里先默认标题高度为30 item高度为100
   console.log(this.data.cakeTypes)
   let titleItemH=[]
   for(let i=0;i<this.data.cakeTypes.length;i++){
     let titleH=30
     let itemH= this.data.cakeTypes[i].productIds.length*100
     let dd={}
     dd.typeId = this.data.cakeTypes[i].typeId
     dd.titleItemH = titleH + itemH
     titleItemH.push(dd)
   }
   console.log(titleItemH)
   this.setData({ titleItemH: titleItemH})
 },
  onGoodsScroll: function (e) {
    let self=this
    if (scrollTimer){
      clearTimeout(scrollTimer)
    }
    console.log(e)
    //e.detail.scrollTop 滚动距离
    var scrollTop = e.detail.scrollTop;
    var h = 0;
    //左边菜单那个要亮 点个全局变量classifySeleted 根据滚动高度去置换它，决定哪边亮
    var classifySeleted = this.data.classifySeleted;
// 当前元素 当前元素的索引值 这个cakeTypes是右边的数据

console.log(this.data.cakeTypes)
    this.data.titleItemH.forEach(function (classify, i) {
      console.log("h:" + h + " scrollTop:" + scrollTop);
      console.log(classify)
      //标题高度+加整个元素的高度
      var _h = classify.titleItemH;
      console.log(_h)
      // 这可通过调节高度决定滚到哪里
      if (scrollTop >= h - 10) {
        classifySeleted = classify.typeId;
      }
      //得到所有的高度
      h += _h;
    });

    // this.setData({
    //   classifySeleted: classifySeleted
    // });
//isTap只是判断它是否点击过
    if (this.data.isTap) {
      this.setData({
        isTap: false, classifySeleted: classifySeleted
      })
    } else {
      this.setData({
        classifySeleted: classifySeleted
      });
    }
    
  },

  tapClassify: function (e) {
    console.log(e.currentTarget.dataset.id)
    let id = e.currentTarget.dataset.id
    this.setData({
      isTap: true,
      classifySeleted: id,
      typeIndex: id
    });
  },
})
